package cn.edu.zafu.bigdata.bd121.student.utils;

import cn.edu.zafu.bigdata.bd121.student.cli.CliMain;
import cn.edu.zafu.bigdata.bd121.student.constant.StudentInfoConst;
import cn.edu.zafu.bigdata.bd121.student.entity.StudentPO;

import java.util.Objects;
import java.util.Scanner;

/**
 * 读取用户输入的工具类
 *
 * @author 202211090121
 */
public class InputUtils {

    /**
     * 读取学生id
     *
     * @param isCreate 是否为创建学生
     * @return 学生id
     */
    public static String getValidId(boolean isCreate) {
        Scanner scanner = CliMain.scanner;
        String id;
        while (true) {
            System.out.print("请输入学生学号：（10位数字）");
            id = scanner.nextLine();
            if (!id.matches(StudentInfoConst.ID_REGEX)) {
                System.out.println("学号格式不正确，请重新输入！");
                continue;
            }
            if (isCreate) {
                final String currentId = id;
                if (CliMain.studentList.stream().anyMatch(studentPo -> studentPo.getId().equals(currentId))) {
                    System.out.println("学号已存在，请重新输入！");
                } else {
                    break;
                }
            }
        }
        return id;
    }

    /**
     * 读取学生对象并允许退出
     *
     * @return 学生对象
     */
    public static StudentPO getStudentPo() {
        Scanner scanner = CliMain.scanner;
        String id;
        while (true) {
            System.out.print("请输入学生 ID (或输入 QUIT 退出)：");
            id = scanner.nextLine();

            // 允许用户在输入时退出
            if ("QUIT".equalsIgnoreCase(id)) {
                System.err.println("退出删除操作。");
                return null;
            }
            if (!id.matches(StudentInfoConst.ID_REGEX)) {
                System.out.println("学号格式不正确，请重新输入！");
            } else {
                break;
            }
        }
        // 查找对应的学生信息
        StudentPO chosenStudent = null;
        for (StudentPO student : CliMain.studentList) {
            if (Objects.equals(student.getId(), id)) {
                chosenStudent = student;
                break;
            }
        }
        return chosenStudent;
    }

    /**
     * 读取学生姓名
     *
     * @return 学生姓名
     */
    public static String getValidName() {
        Scanner scanner = CliMain.scanner;
        String name;
        while (true) {
            System.out.print("请输入学生姓名：（2-3位中文字符）");
            name = scanner.nextLine();
            if (name.matches(StudentInfoConst.NAME_REGEX)) {
                break;
            }
            System.out.println("姓名格式不正确，请重新输入！");
        }
        return name;
    }

    /**
     * 读取学生电话
     *
     * @return 学生电话
     */
    public static String getValidPhone() {
        Scanner scanner = CliMain.scanner;
        String phone;
        while (true) {
            System.out.print("请输入学生手机号：（11位数字）");
            phone = scanner.nextLine();
            if (phone.matches(StudentInfoConst.PHONE_REGEX)) {
                break;
            }
            System.out.println("手机号格式不正确，请重新输入！");
        }
        return phone;
    }

    public static boolean confirm() {
        System.out.print("确认执行此操作？这将无法撤销！(Y/N): ");
        String answer = CliMain.scanner.nextLine();
        return "Y".equalsIgnoreCase(answer);
    }
}
